package com.sxkf.template.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.constants.OrderConstant;
import com.sxkf.template.entity.Order;
import com.sxkf.template.entity.OrderDetail;
import com.sxkf.template.entity.Refund;
import com.sxkf.template.service.OrderService;
import com.sxkf.template.vo.request.order.CreateOrderVo;
import com.sxkf.template.vo.request.order.DeliverOrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
* 订单表(t_order)表控制层
*
* @author 大佬zgc
*/
@Api(tags="订单模块")
@RestController
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    //todo 确认收货，后台发货待做(到时候看客户要不要接第三方物流查询)

    @ApiOperation("从购物车创建订单")
    @PostMapping("/createOrderByCart")
    public R<String> createOrderByCart(@RequestBody CreateOrderVo createOrderVo){
        return R.success(orderService.createOrderByCart(createOrderVo));
    }

    @ApiOperation(value = "创建单个商品订单/再次购买/拼团")
    @PostMapping("/createOrderSingle")
    public R<String> createOrderSingle(@RequestBody CreateOrderVo createOrderVo){
        return R.success(orderService.createOrderSingle(createOrderVo));
    }

    @ApiOperation(value = "去支付")
    @PostMapping(value = "/toPay")
    public R<String> toPay(@RequestParam String orderId,@RequestParam @ApiParam(value = "支付方式（WX=微信app支付，ZFB=支付宝支付）") String payType){
        return R.success(orderService.toPay(orderId,payType));
    }

    @ApiOperation(value = "取消订单")
    @PostMapping(value = "/cancel")
    public R<?> cancel(@RequestParam String orderId){
        orderService.cancel(orderId);
        return R.success();
    }

    @ApiOperation(value = "后台通过订单号退款")
    @PostMapping(value = "/refundByOrderNo")
    public R<?> refundByOrderNo(@RequestParam String orderNo, @RequestParam BigDecimal refundPrice){
        // todo
        orderService.refundOrder(orderNo,refundPrice);
        return R.success();
    }

    @ApiOperation(value = "提醒发货")
    @PostMapping(value = "/notice")
    public R<?> notice(@RequestParam String orderId){
        // 发一条私信
        orderService.notice(orderId);
        // 商家id

        return R.success();
    }

    @ApiOperation(value = "查询订单详情")
    @GetMapping(value = "/getOrderDetail")
    public R<Order> getOrderDetail(@RequestParam String orderId){
        return R.success(orderService.getOrderDetail(orderId));
    }

    @ApiOperation(value = "用户查询普通订单列表")
    @GetMapping(value = "/userGetList")
    public R<IPage<Order>> userGetList(@RequestParam Integer pageNo,
                                       @RequestParam Integer pageSize,
                                       @RequestParam String userId,
                                       @RequestParam(required = false) String orderId,
                                       @ApiParam("已取消=-1 未支付=0 已支付/待发货=1 配送中/运输中=2 已完成/待评价=3 已评价=4 售后中=5 已退款=6 售后被驳回=7")
                                       @RequestParam(required = false) String state){
        return R.success(orderService.userGetList(pageNo, pageSize, userId, orderId, state));
    }

    @ApiOperation(value = "用户查询拼团订单列表")
    @GetMapping(value = "/spellOutList")
    public R<IPage<Order>> spellOutList(@RequestParam Integer pageNo,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String userId,
                                        @ApiParam("订单状态：已取消=-1 未支付=0 已支付/待发货=1 配送中/运输中=2 已完成/待评价=3 已评价=4 售后中=5 已退款=6 售后被驳回=7")
                                        @RequestParam(required = false) String orderState,
                                        @ApiParam("拼团状态：(0=未拼成，1=已拼成，2=已过期)")
                                        @RequestParam(required = false) String spellOutState){
        return R.success(orderService.spellOutList(pageNo, pageSize, userId, orderState, spellOutState));
    }


    @ApiOperation(value = "后台查询拼团订单列表")
    @GetMapping(value = "/backSpellOutList")
    public R<IPage<OrderDetail>> backSpellOutList(@RequestParam Integer pageNo,
                                                  @RequestParam Integer pageSize,
                                                  @ApiParam("商家用户id")
                                                      @RequestParam String busUserId,
                                                  @ApiParam("订单状态：已取消=-1 未支付=0 已支付/待发货=1 配送中/运输中=2 已完成/待评价=3 已评价=4 售后中=5 已退款=6 售后被驳回=7")
                                                      @RequestParam(required = false) String orderState,
                                                  @ApiParam("拼团状态：(0=未拼成，1=已拼成，2=已过期)")
                                                      @RequestParam(required = false) String spellOutState){
        return R.success(orderService.backSpellOutList(pageNo, pageSize, busUserId, orderState, spellOutState));
    }


    @ApiOperation(value = "商家查询订单列表")
    @GetMapping(value = "/busGetList")
    public R<IPage<Order>> busGetList(@RequestParam Integer pageNo,
                                      @RequestParam Integer pageSize,
                                      @ApiParam("店铺id")
                                      @RequestParam String storeId,
                                      @RequestParam(required = false) String orderId,
                                      @ApiParam("是否拼团 1=是 0=否")
                                      @RequestParam(required = false) String spellOut,
                                      @ApiParam("已取消=-1 未支付=0 已支付/待发货=1 配送中/运输中=2 已完成/待评价=3 已评价=4 售后中=5 已退款=6 售后被驳回=7")
                                      @RequestParam(required = false) String state){
        return R.success(orderService.busGetList(pageNo, pageSize, storeId, orderId, spellOut, state));
    }

    @ApiOperation("后台发货")
    @PostMapping(value = "/deliver")
    public R<?> deliver(@RequestBody DeliverOrderVo deliverOrderVo){

        Boolean res = orderService.deliver(deliverOrderVo);

        return R.success(res);
    }

    @ApiOperation("确认收货")
    @GetMapping(value = "/rerv")
    public R<?> rerv(@RequestParam(required = false) String userId,
                     @RequestParam String orderId){
        Boolean res = orderService.rerv(userId,orderId);
        return R.success(res);
    }


//    @ApiOperation("申请退款")
//    @PostMapping(value = "/refund")
//    public R<?> refund(@RequestBody Refund refund){
//        // todo 得改
//        Boolean res = orderService.refund(refund);
//        return R.success(res);
//    }

    @ApiOperation("驳回退款")
    @GetMapping(value = "/reject")
    public R<?> reject(@RequestParam String refundId){
        Boolean res = orderService.reject(refundId);
        return R.success(res);
    }

    @ApiOperation("退款申请状态")
    @GetMapping(value = "/refundStatus")
    public R<?> refundStatus(@RequestParam String refundId){
        String res = orderService.refundStatus(refundId);
        return R.success(res);
    }

    @ApiOperation("删除订单")
    @GetMapping(value = "/delete")
    public R<String> delete(@RequestParam String orderId){
        Boolean res = orderService.delete(orderId);
        if (res){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }

    @ApiOperation("待评价订单")
    @GetMapping(value = "/waitComment")
    public R<IPage<Order>> waitComment(@RequestParam Integer pageNo,
                                       @RequestParam Integer pageSize,
                                       @RequestParam String userId){

        Page<Order> page = orderService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<Order>()
                .eq(Order::getState, OrderConstant.FINISH)
                .eq(Order::getUserId, userId)
        );

        return R.success(page);

    }






}   
